home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-68k-src / machines / amiga68k / libsrc / genauto.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  3KB  |  123 lines

  1. /*
  2. ** genauto.c
  3. **
  4. ** Creates C source with INIT/EXIT functions for automatic opening
  5. ** and closing of libraries.
  6. **
  7. ** V0.3  20-Oct-98  phx
  8. **       Library version may be defined by the user. For example:
  9. **         unsigned long _XyzBaseVer = 99;
  10. **       otherwise, a version of 0 will be used.
  11. **       A second argument is allowed to define the priority of the
  12. **       INIT/EXIT functions.
  13. ** V0.2  07-Oct-98  vb
  14. **       _INIT, _EXIT, no printf (requires vlink 0.5f)
  15. ** V0.1  06-Oct-98  phx
  16. **       created
  17. **
  18. */
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #define VERSION 0
  24. #define REVISION 3
  25. #define BUFSIZE 256
  26. #define DEFPRI 5  /* default priority for INIT/EXIT functions */
  27.  
  28.  
  29. main(int argc,char *argv[])
  30. {
  31.   char linebuf[BUFSIZE];
  32.   char namebuf[BUFSIZE];
  33.   char *p,*q,*base = NULL;
  34.   FILE *f;
  35.   int n;
  36.   int pri = DEFPRI;
  37.  
  38.   if (argc<2 || argc>3) {
  39.     printf("genauto V%d.%d\nusage: %s <FD file> [<defpri>]\n",
  40.            VERSION,REVISION,argv[0]);
  41.     exit(1);
  42.   }
  43.   if (argc==3)
  44.     pri = atoi(argv[2]);  /* override default priority */
  45.  
  46.   if (f = fopen(argv[1],"r")) {
  47.  
  48.     /* determine library name from FD file name */
  49.     /* Example: FD:graphics_lib.fd  -->  graphics.library */
  50.     n = strlen(argv[1]);
  51.     p = argv[1]+(n-1);
  52.     q = namebuf;
  53.     while (n--) {
  54.       if (*p=='/' || *p==':')
  55.         break;
  56.       p--;
  57.     }
  58.     p++;
  59.     while (isalnum((unsigned)*p))
  60.       *q++ = *p++;
  61.     strcpy(q,".library");
  62.  
  63.     /* get base name from FD file */
  64.     while (fgets(linebuf,BUFSIZE-1,f)) {
  65.       if (!strncmp(linebuf,"##base",6)) {
  66.         p = linebuf+6;
  67.         while (*p && *p!='_')
  68.           p++;
  69.         if (*p) {
  70.           base = ++p;
  71.           while (isgraph((unsigned)*p))
  72.             p++;
  73.           strcpy(p,".c");
  74.         }
  75.         break;
  76.       }
  77.     }
  78.     fclose(f);
  79.     if (!base) {
  80.       fprintf(stderr,"No valid base definition found in %s\n",argv[1]);
  81.       exit(10);
  82.     }
  83.  
  84.     /* generate C source */
  85.     if (f = fopen(base,"w")) {
  86.       *p = 0;  /* remove ".c" from base */
  87.  
  88.       fprintf(f,"#include <exec/libraries.h>\n"
  89.                 "#include <proto/exec.h>\n\n"
  90.                 "struct Library *%s = NULL;\n"
  91.                 "extern unsigned long _%sVer;\n\n"
  92.                 "void _INIT_%d_%s()\n{\n"
  93.                 "  if (!(%s = OpenLibrary(\"%s\",_%sVer)))\n"
  94.                 "    exit(20);\n}\n\n"
  95.                 "void _EXIT_%d_%s()\n{\n"
  96.                 "  if (%s)\n    CloseLibrary(%s);\n}\n",
  97.                 base,base,pri,base,base,namebuf,base,pri,base,base,base);
  98.       fclose(f);
  99.     }
  100.     else {
  101.       fprintf(stderr,"Can't create main source file %s\n",base);
  102.       exit(10);
  103.     }
  104.  
  105.     /* a second source for the library version */
  106.     sprintf(namebuf,"%sVer.c",base);
  107.     if (f = fopen(namebuf,"w")) {
  108.       fprintf(f,"unsigned long _%sVer = 0;\n",base);
  109.       fclose(f);
  110.     }
  111.     else {
  112.       fprintf(stderr,"Can't create version source file %s\n",namebuf);
  113.       exit(10);
  114.     }
  115.   }
  116.  
  117.   else {
  118.     fprintf(stderr,"Can't read FD file: %s\n",argv[1]);
  119.     exit(10);
  120.   }
  121.   exit(0);
  122. }
  123.